#include "../hle.h"

#include "__ThreadManForUser.h"
#include "__sceUmdUser.h"

/* s32 sceUmdCheckMedium(s32 a); */
void sceUmdCheckMedium()
{
    _log(DBG, HLE, "1 = sceUmdCheckMedium()");
    RETURN(1); /* non-zero: disc in drive */
}

void sceUmdGetDiscInfo()
{
    _log(ERR, HLE, "sceUmdGetDiscInfo(?)");
    RETURN(0);
}

void sceUmdActivate()
{
    char *name = mem_readStr(PARAM(1));
    _log(INF, HLE, "0 = sceUmdActivate(%08x, %s)", PARAM(0), name);
    __KernelUmdActivate();
    __KernelResumeThreads(WAITTYPE_UMD, __KernelUmdGetState(), RESUMETYPE_VALUE_OR);
    RETURN(0);
    mem_freeStr(name);
}

void sceUmdDeactivate()
{
    char *name = mem_readStr(PARAM(1));
    _log(INF, HLE, "sceUmdDeactivate(%08x, %s)", PARAM(0), name);
    __KernelUmdDeactivate();
    RETURN(0);
    mem_freeStr(name);
}

void sceUmdRegisterUMDCallBack()
{
    _log(INF, HLE, "0 = sceUmdRegisterUMDCallback(id = %d)", PARAM(0));
    __KernelRegisterCallback(CBTYPE_UMD, PARAM(0));
    RETURN(0);
}

void sceUmdUnRegisterUMDCallBack()
{
    if (__KernelCallbackExists(PARAM(0)))
    {
        _log(INF, HLE, "0 = sceUmdUnRegisterUMDCallback(id = %d)", PARAM(0));
        __KernelUnregisterCallback(CBTYPE_UMD);
        RETURN(0);
    }
    else {
        _log(ERR, HLE, "sceUmdUnRegisterUMDCallback(id = %d): invalid callback", PARAM(0));
        RETURN(SCE_ERROR_NOT_FOUND_CALLBACK);
    }
}

void sceUmdGetDriveStat()
{
    u8 state = __KernelUmdGetState();
    _log(DBG, HLE, "0x%02x = sceUmdGetDriveStat()", state);
    RETURN(state);
}

void sceUmdWaitDriveStat()
{
    u32 stat = PARAM(0);
    _log(DBG, HLE, "0 = sceUmdWaitDriveStat(stat = 0x%02x)", stat);
    if ((stat & __KernelUmdGetState()) != stat)
        __KernelWaitCurThread(WAITTYPE_UMD, 0, stat, 0, 0);
    RETURN(0);
}

void sceUmdWaitDriveStatWithTimer()
{
    u32 stat = PARAM(0);
    u32 timer = PARAM(1);
    _log(DBG, HLE, "0 = sceUmdWaitDriveStatWithTimer(stat = 0x%02x, timer = %d)", stat, timer);
    if ((stat & __KernelUmdGetState()) != stat)
        __KernelWaitCurThread(WAITTYPE_UMD, 0, stat, timer, 0);
    RETURN(0);
}

void sceUmdWaitDriveStatCB()
{
    u32 stat = PARAM(0);
    _log(DBG, HLE, "0 = sceUmdWaitDriveStatCB(stat = 0x%02x)", stat);
    if ((stat & __KernelUmdGetState()) != stat)
        __KernelWaitCurThread(WAITTYPE_UMD, 0, stat, 0, 1);
    RETURN(0);
}

void sceUmdReplacePermit()
{
    _log(ERR, HLE, "0 = sceUmdReplacePermit()");
    RETURN(0);
}

void sceUmdGetErrorStat()
{
    _log(ERR, HLE, "0 = sceUmdGetErrorStat()");
    RETURN(0);
}

